import proj4 from "proj4";

// 2000坐标转为经纬度:zoneWide几度分带
export const convertToLatLng = (x, y, zoneWide) => {
	let longitude0;
	const a = 6378137.0;
	const iPI = Math.PI / 180;
	// 大地2000坐标系参数
	const f = 1 / 298.257222101;
	// 查找带号
	const projNo = parseInt(x / 1000000, 10);
	if (zoneWide === 3) {
		// 3度带
		longitude0 = projNo * 3;
	} else {
		// 6度带
		longitude0 = (projNo - 1) * zoneWide + zoneWide / 2;
	}
	// 中央经线
	longitude0 = longitude0 * iPI;
	const X0 = projNo * 1000000 + 500000;
	const Y0 = 0;
	const xval = x - X0;
	// 带内大地坐标
	const yval = y - Y0;
	const e2 = 2 * f - f * f;
	const e1 = (1.0 - Math.sqrt(1 - e2)) / (1.0 + Math.sqrt(1 - e2));
	const ee = e2 / (1 - e2);
	const M = yval;
	const u =
		M / (a * (1 - e2 / 4 - (3 * e2 * e2) / 64 - (5 * e2 * e2 * e2) / 256));
	const fai =
		u +
		((3 * e1) / 2 - (27 * e1 * e1 * e1) / 32) * Math.sin(2 * u) +
		((21 * e1 * e1) / 16 - (55 * e1 * e1 * e1 * e1) / 32) * Math.sin(4 * u) +
		((151 * e1 * e1 * e1) / 96) * Math.sin(6 * u) +
		((1097 * e1 * e1 * e1 * e1) / 512) * Math.sin(8 * u);
	const C = ee * Math.cos(fai) * Math.cos(fai);
	const T = Math.tan(fai) * Math.tan(fai);
	const NN = a / Math.sqrt(1.0 - e2 * Math.sin(fai) * Math.sin(fai));
	const R =
		(a * (1 - e2)) /
		Math.sqrt(
			(1 - e2 * Math.sin(fai) * Math.sin(fai)) *
				(1 - e2 * Math.sin(fai) * Math.sin(fai)) *
				(1 - e2 * Math.sin(fai) * Math.sin(fai))
		);
	const D = xval / NN;
	// 计算经度(Longitude) 纬度(Latitude)
	const longitude1 =
		longitude0 +
		(D -
			((1 + 2 * T + C) * D * D * D) / 6 +
			((5 - 2 * C + 28 * T - 3 * C * C + 8 * ee + 24 * T * T) *
				D *
				D *
				D *
				D *
				D) /
				120) /
			Math.cos(fai);
	const latitude1 =
		fai -
		((NN * Math.tan(fai)) / R) *
			((D * D) / 2 -
				((5 + 3 * T + 10 * C - 4 * C * C - 9 * ee) * D * D * D * D) / 24 +
				((61 + 90 * T + 298 * C + 45 * T * T - 256 * ee - 3 * C * C) *
					D *
					D *
					D *
					D *
					D *
					D) /
					720);
	// 转换为度 DD
	const L_D = longitude1 / iPI;
	const B_D = latitude1 / iPI;
	console.log(
		"iPI:" +
			iPI +
			", a:" +
			a +
			", f:" +
			f +
			", projNo:" +
			projNo +
			",longitude0:" +
			longitude0
	);
	console.log(
		"X0:" +
			X0 +
			", Y0:" +
			Y0 +
			", xVal:" +
			xval +
			", e2:" +
			e2 +
			", e1:" +
			e1 +
			", ee:" +
			ee +
			", M:" +
			M +
			", u: " +
			u +
			", fai:" +
			fai
	);
	// console.log("C:" + C + ", T:" + T + ", NN:" + NN + ", R:" + R + ", D:" + D);
	// console.log(
	// 	"longitude1:" +
	// 		longitude1 +
	// 		",latitude1:" +
	// 		latitude1 +
	// 		", lng:" +
	// 		L_D +
	// 		", lat:" +
	// 		B_D
	// );
	const p = { lng: L_D, lat: B_D };
	return p;
};

// 转大地2000坐标（39带）
export const convertToCGCS2000Point39 = (x, y) => {
	const fromProjection = `+proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=39500000 +y_0=0 +a=6378140 +b=6356755.288157528 +units=m +no_defs `;
	const toProjection = `+proj=longlat +datum=WGS84 +no_defs`;
	const newCoordinates = proj4(toProjection, fromProjection, [x, y]);
	return newCoordinates;
};

// 转大地2000坐标（40带）
export const convertToCGCS2000Point40 = (x, y) => {
	const fromProjection = `+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=40500000 +y_0=0 +a=6378140 +b=6356755.288157528 +units=m +no_defs `;
	const toProjection = `+proj=longlat +datum=WGS84 +no_defs`;
	const newCoordinates = proj4(toProjection, fromProjection, [x, y]);
	return newCoordinates;
};

// 转大地2000坐标（41带）
export const convertToCGCS2000Point41 = (x, y) => {
	const fromProjection = `+proj=tmerc +lat_0=0 +lon_0=123 +k=1 +x_0=41500000 +y_0=0 +a=6378140 +b=6356755.288157528 +units=m +no_defs `;
	const toProjection = `+proj=longlat +datum=WGS84 +no_defs`;
	const newCoordinates = proj4(toProjection, fromProjection, [x, y]);
	return newCoordinates;
};
// 转大地2000坐标（42带）
export const convertToCGCS2000Point42 = (x, y) => {
	const fromProjection = `+proj=tmerc +lat_0=0 +lon_0=126 +k=1 +x_0=42500000 +y_0=0 +a=6378140 +b=6356755.288157528 +units=m +no_defs `;
	const toProjection = `+proj=longlat +datum=WGS84 +no_defs`;
	const newCoordinates = proj4(toProjection, fromProjection, [x, y]);
	return newCoordinates;
};


// 经纬度->大地2000坐标
export const convertToCGCS2000 = (x, y) => {
	if (x >= 115.5 && x <= 118.5) {
		return convertToCGCS2000Point39(x, y);
	}
	if (x > 118.5 && x <= 121.5) {
		return convertToCGCS2000Point40(x, y);
	}
	if (x > 121.5 && x <= 124.5) {
		return convertToCGCS2000Point41(x, y);
	}
	if (x > 124.5 && x <= 127.5) {
		return convertToCGCS2000Point42(x, y);
	}
};
